package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/checkgroup")
@RestController
public class CheckgroupController {

    @Autowired
    private CheckgroupService checkgroupService;

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = checkgroupService.queryPage(queryPageBean);

        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    @PostMapping("/add")
    public Result add(Integer[] checkitemIds,
                      @RequestBody CheckGroup checkGroup) {
        checkgroupService.add(checkGroup, checkitemIds);

        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        CheckGroup checkGroup = checkgroupService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer id){
        List<Integer> checkItems = checkgroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkItems);
    }

    @PostMapping("/edit")
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        checkgroupService.edit(checkitemIds,checkGroup);
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(@RequestParam("id") Integer id){
        checkgroupService.deleteCheckGroupitemById(id);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkgroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }
}
